//
//  KFReportCell.m
//  KafeeSale
//
//  Created by Liboom on 2025/11/9.
//

#import "KFReportCell.h"
@interface KFReportCell ()

@property (nonatomic, strong) UILabel *beanNameLabel;
@property (nonatomic, strong) UILabel *weightLabel;
@property (nonatomic, strong) UILabel *beanValueLabel;
@property (nonatomic, strong) UILabel *stockLab;

@end
@implementation KFReportCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    // 豆子名称标签
    _beanNameLabel = [[UILabel alloc] init];
    _beanNameLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
    _beanNameLabel.textColor = [UIColor darkTextColor];
    [self.contentView addSubview:_beanNameLabel];
    
    // 重量标签
    _weightLabel = [[UILabel alloc] init];
    _weightLabel.font = [UIFont systemFontOfSize:15];
    _weightLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_weightLabel];
    
    // 豆子英文值标签
    _beanValueLabel = [[UILabel alloc] init];
    _beanValueLabel.font = [UIFont systemFontOfSize:15];
    _beanValueLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_beanValueLabel];
    
    // 使用SDAutoLayout布局
    _beanNameLabel.sd_layout
    .topSpaceToView(self.contentView, 10)
    .leftSpaceToView(self.contentView, 15)
    .rightSpaceToView(self.contentView, 15)
    .heightIs(20);
    
    
    _weightLabel.sd_layout
    .topSpaceToView(_beanNameLabel, 5)
    .leftSpaceToView(self.contentView, 15)
    .heightIs(17);
    [_weightLabel setSingleLineAutoResizeWithMaxWidth:300];
    
    _beanValueLabel.sd_layout
    .topSpaceToView(_beanNameLabel, 5)
    .rightSpaceToView(self.contentView, 15)
    .heightIs(17);
    [_beanValueLabel setSingleLineAutoResizeWithMaxWidth:300];
}

- (void)configureWithData:(NSDictionary *)data {
    // 设置重量和英文值
    _weightLabel.text = [NSString stringWithFormat:@"重量: %@", data[@"weight"] ?: @"0"];
    _beanValueLabel.text = [NSString stringWithFormat:@"库存: %@", data[@"stock"] ?: @""];
    
    // 根据英文值查找中文名称
    NSString *beanChineseName = [self findChineseNameForBean:data[@"bean"]];
    if (beanChineseName) {
        _beanNameLabel.text = beanChineseName;
    } else {
        // 如果没有找到中文名，使用返回的beanName或英文值
        _beanNameLabel.text = data[@"beanName"] ?: data[@"bean"];
    }
}

// 根据豆子的英文值查找中文名称
- (NSString *)findChineseNameForBean:(NSString *)beanValue {
    if (!beanValue) return nil;
    
    VicSingleObject *single = [VicSingleObject getInstance];
    NSArray *beansList = single.beansList;
    
    if (![beansList isKindOfClass:[NSArray class]]) {
        return nil;
    }
    
    for (NSDictionary *beanDict in beansList) {
        if ([beanDict[@"beanValue"] isEqualToString:beanValue]) {
            return beanDict[@"beanName"];
        }
    }
    
    return nil;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
